.org 0xE68D0 ; Terrain Firepower

Skills_Terrain_Firepower:

	PUSH 	{R4-R8, LR}
	
	LDR 	R5, [Terrain_Firepower_1]
	LDR 	R4, [Terrain_Firepower_2]
	LDR 	R5, [R5, 0]
	LDR 	R4, [R4, 0]
	MOV 	R7, R1
	ADD 	R1, R5, R4, LSL 2
	LDR 	R1, [R1, 0x48]
	MOV 	R8, R0
	ADD 	R1, R1, 0x4000
	LDR 	R4, [R1, 0x3D0]
	ADD 	R1, R4, R3, LSL 1
	ADD 	R1, R1, 0x4100
	LDRH 	R1, [R1, 0X68]
	ADD 	R1, R2, R1
	ADD 	R1, R4, R1
	ADD 	R1, R1, 0x1000
	
	LDRB 	R1, [R1, 0x420]
	
	AND 	R6, R1, 0x1F
	MOV 	R1, R6
	BL 		Terrain_Firepower_Sub
	
;-------------------------------------------------------------------------------	
	
	LDR 	R3, [Terrain_Firepower_1]
	LDR 	R2, [Terrain_Firepower_2]
	MOV 	R1, 0x98
	LDR 	R3, [R3, 0]
	LDR 	R2, [R2, 0]
	MUL 	R4, R8, R1
	ADD 	R1, R3, R2, LSL 2
	LDR 	R1, [R1, 0x48]
	ADD 	R5, R0, 0
	ADD 	R0, R1, 0x5000
	LDR 	R0, [R0, 0xA58]
		
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	MOV 	R1, CO_Terrain_Firepower			; Skill: Terrain to Firepower
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped
	
	CMP 	R0, 0								; Does CO use that skill?
	BEQ 	No_Star_Terrain_Firepower			; Skip if CO isn't using that skill
	
	MOV 	R0, R8
	MOV 	R1, R7	
	MOV 	R2, R6
	
	BL 		Double_Terrain_Stars				; Check skills to add Terrain Stars
	
	ADD 	R5, R5, R0							; Add the extra firepower per terrain star (+10%)	

;-------------------------------------------------------------------------------	
	
No_Star_Terrain_Firepower:
	
	B 		Terrain_Type_Check + OL_7_Offset	; Check the terrain the unit is on

;-------------------------------------------------------------------------------	
	
Terrain_Firepower_Setup:

	LDR 	R1, [Terrain_Firepower_1]
	LDR 	R0, [Terrain_Firepower_2]
	LDR 	R2, [R1, 0]
	LDR 	R0, [R0, 0]
	
	ADD 	R0, R2, R0, LSL 2
	LDR 	R0, [R0, 0x48]
	ADD 	R0, R0, 0x5000
	LDR 	R0, [R0, 0xA58]
	ADD 	R0, R0, R4
	ADD 	R0, R0, 0x74
	
	BX 		LR
	
;-------------------------------------------------------------------------------

Road_Rage_Rank:

	BL		Terrain_Firepower_Setup
	
	MOV 	R1, Rank_Road_Rage					; Rank: Road Rage
		
	BL 		Check_Skills_Equipped				; Check the Skill is equipped	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, Stats_Road_Rage				; If CO use skill, add firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------
	
Ranger_Rank:

	BL		Terrain_Firepower_Setup
	
	MOV 	R1, Rank_Ranger						; Rank: Ranger
		
	BL 		Check_Skills_Equipped				; Check the Skill is equipped	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, Stats_Ranger				; If CO use skill, add firepower
	
	B 		No_Terrain_Rank
	
;-------------------------------------------------------------------------------

Mountaineer_Rank:

	BL		Terrain_Firepower_Setup
	
	MOV 	R1, Rank_Mountaineer				; Rank: Mountaineer	
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, Stats_Mountaineer			; If CO use skill, add firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------
	
Urban_Fighter_Rank:	

	BL		Terrain_Firepower_Setup
	
	MOV 	R1, Rank_Urban_Fighter				; Rank: Urban Fighter
	
	BL 		Check_Skills_Equipped				; Check the Skill is equipped	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, Stats_Urban_Fighter			; If CO use skill, add firepower
	
	B 		No_Terrain_Rank

;-------------------------------------------------------------------------------
	
Seamanship_Rank:
	
	BL		Terrain_Firepower_Setup
	
	MOV 	R1, Rank_Seamanship					; Rank: Seamanship
		
	BL 		Check_Skills_Equipped				; Check the Skill is equipped	
	CMP 	R0, 0								; Does CO use that skill?
	ADDNE 	R5, R5, Stats_Seamanship			; If CO use skill, add firepower
	
;-------------------------------------------------------------------------------	
	
No_Terrain_Rank:

	MOV 	R0, R5								; Total Terrain Stars
	
	POP 	{R4-R8, LR}	
	BX 		LR
	
;-------------------------------------------------------------------------------
	
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP	
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	NOP
	
;-------------------------------------------------------------------------------

Terrain_Firepower_1:
	.dw Reg1_Skill
	
Terrain_Firepower_2:
	.dw Reg2_Skill
	
;-------------------------------------------------------------------------------	
	
.org 0xDFCF8
Terrain_Firepower_Sub:	



